<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/25
 * Time: 9:55
 */

namespace App\Http\Controllers\System;


use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('check.login');
    }

    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.user.list',compact('menu_id','toolBarButton'));
    }

    public function getUserList()
    {
        $org_id = request()->get('id');
        try{
            $user_list = callApiServer('System.UserService.getUserList',['org_id'=>$org_id]);
            return response()->json($user_list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }


    public function showAddForm()
    {
        return view('system.user.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('id');
        try{
            $info = callApiServer("System.UserService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.user.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id');
        $params['org_id'] = request('org_id');
        if (request('is_active') === 'on') {
            $params['is_active'] = 1;
        }else{
            $params['is_active'] = 0;
        }
        $params['username'] = request('username');
        $params['realname'] = request('realname');
        $params['sex'] = request('sex');
        $params['phone'] = request('phone');
        $params['email'] = request('email');
        $params['qq'] = request('qq');
        $params['fax'] = request('fax');
        $params['fax'] = request('fax');
//        $params['photo'] = request('photo');
        $params['photo'] = '';
        $params['remark'] = request('remark');
        $params['is_admin'] = request('is_admin',0);

        if(empty($params['username'])){
            return response()->json(['code'=>400,'data'=>'用户名称不能为空']);
        }

        $rs = callApiServer("System.UserService.saveUserInfo", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function remove()
    {
        $params['id'] = request('id');
        $rs = callApiServer("System.UserService.remove", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }

    public function powerIndex()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.power.list',compact('menu_id','toolBarButton'));
    }

    public function getPowerList()
    {
        try{
            $params['page'] = request('page', 1);
            $params['rows'] = request('rows', 10);
            $power_list = callApiServer('System.PowerService.getPowerList',$params);
            return response()->json($power_list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddPower()
    {
        return view('system.power.form');
    }

    public function showEditPower()
    {
        $params['id'] = request('id');
        try{
            $info = callApiServer("System.PowerService.getPowerOne", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.power.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function savePower()
    {
        $params['id'] = request('id');
        $params['code'] = request('code');
        $params['name'] = request('name');
        $params['description'] = request('description');

        $rs = callApiServer("System.PowerService.savePowerInfo", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function removePower()
    {
        $params['id'] = request('id');
        $rs = callApiServer("System.PowerService.removePower", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }

    function getList(){
        try{
            $user_list = callApiServer('System.UserService.getInfoList');
            return response()->json($user_list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    function getUserInfo(){
        $id = request('id');
        try{
            $info = callApiServer('System.UserService.getInfoById', ['id'=>$id]);
            return response()->json($info);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }
}